<?php
class Default_Service_Currency
{

    public static function add($row){
        $obj = new Default_Model_Currency();
        return $obj->add($row);
	}
 
	public static function getByCode($code){
        $obj = new Default_Model_Currency();
        return $obj->getByCode($code);
	}
	/*
	*币种与金额转换
	*$value 转换金额
	*$orgCode 原始货币CODE
	*$disCode 目的货币CODE
	*/
	public static function changeCurrency($value,$orgCode,$disCode){
		$orgCode = strtoupper($orgCode);
		$disCode = strtoupper($disCode);
		$obj = new Default_Model_Currency();		
		$orgCurrency = $obj->getByCode($orgCode);
		 
		if($orgCode==$disCode){
			$result['ask'] = 'Success';
			$result['value'] = $value;
			
			$result['currency_code'] = $disCode;
			$result['org_rate'] = $orgCurrency['currency_rate'];
			$result['dis_rate'] = $orgCurrency['currency_rate'];
			$result['rate'] = 1;
			return $result;
		}
		
        
		$disCurrency = $obj->getByCode($disCode);
		if(!$orgCurrency||!$disCurrency){
			$result = array('ask'=>'Error','message'=>'Can not find origin/destination currency code');
		}else{
			//取小数后两位，不作四舍五入
			$result['ask'] = 'Success';
			$result['org_rate'] = $orgCurrency['currency_rate'];
			$result['dis_rate'] = $disCurrency['currency_rate'];
			$result['rate'] = sprintf("%.2f",$orgCurrency['currency_rate']/$disCurrency['currency_rate']);
			$result['value'] = sprintf("%.2f",$value*$result['rate']);
			$result['currency_code'] = $disCode;
		}
        return $result;
	}
}